package shen.yue.example.one.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import shen.yue.example.one.BaseActivity;
import shen.yue.example.one.R;

/**
 * Created by jiangzhuolei on 2017/8/2.
 */

public class Activity4 extends BaseActivity {

    private ViewGroup viewGroup;
    private int left, right, top, bottom;
    private int left1, right1, top1, bottom1;
    private int left2, right2, top2, bottom2;

    private int lastX;
    private int lastY;
    private int startX = 0;
    private int startY = 0;
    private int endX = 0;
    private int endY = 0;
    private int edtextX, edtextY;
    private EditText viewById;
    private boolean isShow = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity4);
        viewGroup = (ViewGroup) findViewById(R.id.root);

        viewById = (EditText) findViewById(R.id.id_text);
        viewById.setOnTouchListener(new EditTextOnTouchListener());
        viewById.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                isShow = b;
                if (b){
                    Log.i(TAG,"viewById boolean:"+b);
                }else {
                    Log.i(TAG,"viewById boolean:"+b);
                }
            }
        });
    }

    class EditTextOnTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (!isShow) {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i(TAG, "ACTION_DOWN");
                        left = viewGroup.getLeft();
                        right = viewGroup.getRight();
                        top = viewGroup.getTop();
                        bottom = viewGroup.getBottom();
                        Log.w(TAG, "left:" + left + "---" + "right:" + right + "---" + "top:" + top + "---" + "bottom:" + bottom);
//
//                        left1 = ll_activity4.getLeft();
//                        right1 = ll_activity4.getRight();
//                        top1 = ll_activity4.getTop();
//                        bottom1 = ll_activity4.getBottom();
//                        Log.w(TAG, "left1:" + left1 + "---" + "right1:" + right1 + "---" + "top1:" + top1 + "---" + "bottom1:" + bottom1);

                        left2 = viewById.getLeft();
                        right2 = viewById.getRight();
                        top2 = viewById.getTop();
                        bottom2 = viewById.getBottom();
                        Log.w(TAG, "left2:" + left2 + "---" + "right2:" + right2 + "---" + "top2:" + top2 + "---" + "bottom2:" + bottom2);

                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i(TAG, "ACTION_UP");
                        endX = (int) event.getRawX();
                        endY = (int) event.getRawY();
                        Log.i(TAG, "startX:" + startX + "---" + "startY:" + startY + "---" + "endX:" + endX + "---" + "endY:" + endY);
//                if(Math.abs(endX-startX)<10&&Math.abs(endY-startY)<10){
//                    return false;
//                }else {
//                    return true;
//                }
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        Log.i(TAG, "ACTION_POINTER_DOWN");
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        Log.i(TAG, "ACTION_POINTER_UP");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i(TAG, "ACTION_MOVE");
//                    int x2 = (int) event.getRawX();
//                    int y2 = (int) event.getRawY();
//                    //让包含edittext的linearlayout随手指的移动而改变位置
//                    ll_activity4.scrollBy(lastX - x2, lastY - y2);
//                    lastX = x2;
//                    lastY = y2;
                        int dx = (int) event.getRawX() - lastX;
                        int dy = (int) event.getRawY() - lastY;

                        int left2 = view.getLeft() + dx;
                        int top2 = view.getTop() + dy;
                        int right2 = view.getRight() + dx;
                        int bottom2 = view.getBottom() + dy;
                        if (left2 < left) {
                            left2 = left;
                            right2 = left2 + view.getWidth();
                        }
                        if (right2 > right) {
                            right2 = right;
                            left2 = right - view.getWidth();
                        }
                        if (top2 < top) {
                            top2 = top;
                            bottom2 = top2 + view.getHeight();
                        }
                        if (bottom2 > bottom) {
                            bottom2 = bottom;
                            top2 = bottom2 - view.getHeight();
                        }
                        view.layout(left2, top2, right2, bottom2);
                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        Log.w(TAG, "lastX:" + lastX);
                        Log.w(TAG, "lastY:" + lastY);
                        break;
                }
                return true;
            } else {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {
                        edtextX = (int) event.getRawX();
                        edtextY = (int) event.getRawY();
                        break;
                    }
                    case MotionEvent.ACTION_MOVE:
                    {
                        int x2 = (int) event.getRawX();
                        int y2 = (int) event.getRawY();
                        //让包含edittext的linearlayout随手指的移动而改变位置
                        viewById.scrollBy(edtextX - x2, edtextY - y2);
                        edtextX = x2;
                        edtextY = y2;
                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    {
                        break;
                    }
                }
                return false;
            }
        }
    }

    class LinearLayoutOnTouchListener implements View.OnTouchListener {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if (true) {

                switch (event.getAction() & MotionEvent.ACTION_MASK) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i(TAG, "ACTION_DOWN");
                        left = viewGroup.getLeft();
                        right = viewGroup.getRight();
                        top = viewGroup.getTop();
                        bottom = viewGroup.getBottom();
                        Log.w(TAG, "left:" + left + "---" + "right:" + right + "---" + "top:" + top + "---" + "bottom:" + bottom);

//                        left1 = ll_activity4.getLeft();
//                        right1 = ll_activity4.getRight();
//                        top1 = ll_activity4.getTop();
//                        bottom1 = ll_activity4.getBottom();
//                        Log.w(TAG, "left1:" + left1 + "---" + "right1:" + right1 + "---" + "top1:" + top1 + "---" + "bottom1:" + bottom1);

                        left2 = viewById.getLeft();
                        right2 = viewById.getRight();
                        top2 = viewById.getTop();
                        bottom2 = viewById.getBottom();
                        Log.w(TAG, "left2:" + left2 + "---" + "right2:" + right2 + "---" + "top2:" + top2 + "---" + "bottom2:" + bottom2);

                        lastX = (int) event.getRawX();
                        lastY = (int) event.getRawY();
                        startX = (int) event.getRawX();
                        startY = (int) event.getRawY();
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.i(TAG, "ACTION_UP");
                        endX = (int) event.getRawX();
                        endY = (int) event.getRawY();
                        Log.i(TAG, "startX:" + startX + "---" + "startY:" + startY + "---" + "endX:" + endX + "---" + "endY:" + endY);
//                if(Math.abs(endX-startX)<10&&Math.abs(endY-startY)<10){
//                    return false;
//                }else {
//                    return true;
//                }
                        break;
                    case MotionEvent.ACTION_POINTER_DOWN:
                        Log.i(TAG, "ACTION_POINTER_DOWN");
                        break;
                    case MotionEvent.ACTION_POINTER_UP:
                        Log.i(TAG, "ACTION_POINTER_UP");
                        break;
                    case MotionEvent.ACTION_MOVE:
                        Log.i(TAG, "ACTION_MOVE");
                        int x2 = (int) event.getRawX();
                        int y2 = (int) event.getRawY();
                        //让包含edittext的linearlayout随手指的移动而改变位置
//                        ll_activity4.scrollBy(lastX - x2, lastY - y2);
                        lastX = x2;
                        lastY = y2;
//                        int dx = (int) event.getRawX() - lastX;
//                        int dy = (int) event.getRawY() - lastY;
//
//                        int left2 = view.getLeft() + dx;
//                        int top2 = view.getTop() + dy;
//                        int right2 = view.getRight() + dx;
//                        int bottom2 = view.getBottom() + dy;
//                        if (left2 < left) {
//                            left2 = left;
//                            right2 = left2 + view.getWidth();
//                        }
//                        if (right2 > right) {
//                            right2 = right;
//                            left2 = right - view.getWidth();
//                        }
//                        if (top2 < top) {
//                            top2 = top;
//                            bottom2 = top2 + view.getHeight();
//                        }
//                        if (bottom2 > bottom) {
//                            bottom2 = bottom;
//                            top2 = bottom2 - view.getHeight();
//                        }
//                        view.layout(left2, top2, right2, bottom2);
//                        lastX = (int) event.getRawX();
//                        lastY = (int) event.getRawY();
//                        Log.w(TAG, "lastX:" + lastX);
//                        Log.w(TAG, "lastY:" + lastY);
                        break;
                }
                return true;
            } else {
                return false;
            }
        }
    }
}
